﻿using Blazui.Component;
using Blazui.Component.Model;
using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore;
using Project13776616780_01.Model;
using Project13776616780_01.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace Project13776616780_01.Pages.Size
{
    public partial class List
    {
        [Inject]
        private DocsDbContext dbContext { get; set; }
        private List<SizeModel> datas = new List<SizeModel>();

        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();
            await RefreshAsync();
        }

        private async Task RefreshAsync()
        {
            datas = (await dbContext.Set<Model.Size>().OrderBy(x => x.Id).ToListAsync()).Select(x => new SizeModel()
            {
                Description = x.Description,
                Name = x.Name,
                Id = x.Id
            }).ToList();
            await InvokeAsync(Refresh);
        }

        private async Task ShowCreateWindowAsync()
        {
            var result = await DialogService.ShowDialogAsync<Create>("创建尺寸");
            await RefreshAsync();
        }

        private async Task ShowUpdateWindowAsync(object data)
        {
            var parameters = new Dictionary<string, object>();
            parameters.Add(nameof(Update.Data), data);
            var result = await DialogService.ShowDialogAsync<Update>("修改尺寸", parameters);
            await RefreshAsync();
        }

        private async Task DeleteAsync(object category)
        {
            var confirmResult = await ConfirmAsync("这将删除该尺寸下的所有藏品，确认？");
            if (confirmResult != MessageBoxResult.Ok)
            {
                return;
            }
            var existCategory = (SizeModel)category;
            dbContext.Sizes.RemoveRange(dbContext.Sizes.Where(x => x.Id == existCategory.Id));
            dbContext.Products.RemoveRange(dbContext.Products.Where(x => x.SizeId == existCategory.Id));
            await dbContext.SaveChangesAsync();
            await RefreshAsync();
        }

        //private async Task SaveDataAsync(TableSaveEventArgs e)
        //{
        //    var kvModel = e.Data as KeyValueModel;
        //    Category existCategory = null;
        //    switch (e.Action)
        //    {
        //        case SaveAction.Create:
        //            existCategory = await dbContext.Set<Category>().FirstOrDefaultAsync(x => x.Name == kvModel.Value);
        //            if (existCategory != null)
        //            {
        //                Toast("名称重复");
        //                e.Cancel = true;
        //                return;
        //            }
        //            existCategory = new Category()
        //            {
        //                Name = kvModel.Value
        //            };
        //            dbContext.Set<Category>().Add(existCategory);
        //            await dbContext.SaveChangesAsync();
        //            categories.Add(new KeyValueModel
        //            {
        //                Key = existCategory.Id.ToString(),
        //                Value = existCategory.Name
        //            });
        //            break;
        //        case SaveAction.Update:
        //            var key = Convert.ToInt32(kvModel.Key);
        //            existCategory = await dbContext.Set<Category>().FirstOrDefaultAsync(x => x.Name == kvModel.Value && x.Id != key);
        //            if (existCategory != null)
        //            {
        //                Toast("名称重复");
        //                e.Cancel = true;
        //                return;
        //            }
        //            existCategory = await dbContext.Set<Category>().FirstOrDefaultAsync(x => x.Id == key);
        //            existCategory.Name = kvModel.Value;
        //            await dbContext.SaveChangesAsync();
        //            categories.FirstOrDefault(x => x.Key == kvModel.Key).Value = kvModel.Value;
        //            break;
        //        case SaveAction.Delete:
        //            categories.RemoveAll(x => x.Key == kvModel.Key);
        //            var key1 = Convert.ToInt32(kvModel.Key);
        //            existCategory = await dbContext.Set<Category>().FirstOrDefaultAsync(x => x.Id == key1);
        //            if (existCategory == null)
        //            {
        //                return;
        //            }
        //            dbContext.Set<Category>().Remove(existCategory);
        //            await dbContext.SaveChangesAsync();
        //            break;
        //    }
        //    MarkAsRequireRender();
        //}
    }
}
